﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using MarketOnlineDTO;
using MySql.Data.MySqlClient;

namespace MarketOnlineDAO
{
    public class DonViTinhDAO : BaseDataProvider
    {
        MySqlCommand _cmd;

        public Boolean ThemDonViTinh(DonViTinhDTO dvt)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "insert into donvitinh(TenDonViTinh) values(@TenDonViTinh)";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@TenDonViTinh",dvt.TenDonViTinh));
            
            return ExecuteNonQuery(_cmd);
        }

        public Boolean XoaDonViTinh(DonViTinhDTO dvt)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete from DonViTinh where MaDonViTinh = @MaDonViTinh";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaDonViTinh", dvt.MaDonViTinh));

            return ExecuteNonQuery(_cmd);
        }

        public Boolean CapNhatDonViTinh(DonViTinhDTO dvt)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update DonViTinh set TenDonViTinh = @TenDonViTinh where MaDonViTinh = @MaDonViTinh";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaDonViTinh", dvt.MaDonViTinh));
            _cmd.Parameters.Add(new MySqlParameter("@TenDonViTinh", dvt.TenDonViTinh));

            return ExecuteNonQuery(_cmd);
        }

        public List<DonViTinhDTO> LayDanhSachDonViTinh()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from DonViTinh";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            { 
                return null;
            }

            List<DonViTinhDTO> list = new List<DonViTinhDTO>();

            foreach (DataRow row in dt.Rows)
            {
                DonViTinhDTO dto = new DonViTinhDTO();
                dto.MaDonViTinh = int.Parse(row["MaDonViTinh"].ToString());
                dto.TenDonViTinh = row["TenDonViTinh"].ToString();

                list.Add(dto);
            }

            return list;
        }

        //@Trọng Võ
        public DonViTinhDTO LayDonViTinh(int MaDonViTinh)//tested
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from DonViTinh where MaDonViTinh=@MaDonViTinh";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaDonViTinh", MaDonViTinh));

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            DataRow row = dt.Rows[0];

            DonViTinhDTO dto = new DonViTinhDTO();
            dto.MaDonViTinh = int.Parse(row["MaDonViTinh"].ToString());
            dto.TenDonViTinh = row["TenDonViTinh"].ToString();
            
            return dto;
        }
        
    }
}
